package com.zxw.hotel.service.impl;

import com.zxw.hotel.dao.KehuDao;
import com.zxw.hotel.dao.ReserveDao;
import com.zxw.hotel.model.GetReserve;
import com.zxw.hotel.model.Kehu;
import com.zxw.hotel.model.clientmumber;
import com.zxw.hotel.model.extend.ReserveExtend;
import com.zxw.hotel.service.KehuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.util.List;

@Service
public class KehuServiceImpl implements KehuService {
    private ReserveDao reserveDao;
    @Autowired
    public void setReserveDao(ReserveDao reserveDao) {
        this.reserveDao = reserveDao;
    }
    private KehuDao kehuDao;
    @Autowired
    public void setKehuDao(KehuDao kehuDao) {
        this.kehuDao = kehuDao;
    }



    @Override
    public List<Kehu> findAllKehu() {
        return kehuDao.findAllKehu();
    }

    @Override
    public clientmumber findByPhone(String phone) {
        return kehuDao.findByPhone(phone);
    }

    @Override
    public int insert(clientmumber account) {
        //通过房间名查询房间id
       Integer[] roomId = kehuDao.findRoomIdByName(account.getRoomName());
       for (int i = 0; i < roomId.length; i++) {
            //尝试使用roomId插入如果过成功就返回 不成功就继续尝试
           ReserveExtend reserveExtend = new ReserveExtend();
           reserveExtend.setRoomId(roomId[i]);
           reserveExtend.setBirthFrom(account.getBirthFrom());
           reserveExtend.setBirthTo(account.getBirthTo());
           reserveExtend.setUserId(account.getUserId());
           try {
               if (reserveDao.insert(reserveExtend) > 0) {
                   return 1;
               }
           } catch (Exception e) {
               // 打印异常日志
               System.out.println("插入数据时发生错误，房间号：" + roomId[i]);
               System.out.println("错误信息：" + e.getMessage());
               // 跳过当前循环的剩余部分，直接进入下一轮循环
               continue;
           }
       }

        return 0;
    }

    @Override
    public List<GetReserve> getreserve(Integer userId) {
        return reserveDao.getreserve(userId);
    }

    @Override
    public int delete(GetReserve getReserve) {
        try{
            int delete = kehuDao.delete(getReserve);
            return delete;
        }catch (Exception e){
            return 0;
        }

    }
}
